local que_jiaofeng = fk.CreateSkill {
    name = "que_jiaofeng",
    anim_type = "control",
}
Fk:loadTranslationTable {
    ["que_jiaofeng"] = "交锋",
    [":que_jiaofeng"] = "其他角色回合开始时，你可以观星X，记录点数总和并弃置；回合结束时，你与其对比其本回合使用，打出或弃置的牌点数总和，" ..
        "较大者可以使用其中一张，且你可以令较小者从剩余的牌中选择一张获得，若使用者不为你，下次发动时额外亮出一张牌(X为你的体力值)。",

    ["@que_jiaofeng-turn"] = "锋",
    ["@@que_jiaofeng-turn"] = "交锋",

    ["@que_jiaofeng_cards"] = "亮出",
}
que_jiaofeng:addEffect(fk.TurnStart, {
    anim_type = "support",
    can_trigger = function(self, event, target, player, data)
        return target ~= player and player:hasSkill(que_jiaofeng.name)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local x = player.hp + player:getMark("que_jiaofeng_cards")
        local cids = room:getNCards(x, "top")
        room:viewCards(player, { cards = cids, skill_name = que_jiaofeng.name })
        local n = 0
        for _, id in ipairs(cids) do
            local num = Fk:getCardById(id).number
            n = n + num
        end
        room:moveCardTo(cids, Card.DiscardPile, nil, fk.ReasonDiscard, que_jiaofeng.name, nil, false, player)
        room:setPlayerMark(player, "@que_jiaofeng-turn", n)
        room:setPlayerMark(target, "@@que_jiaofeng-turn", 1)
        room:setPlayerMark(player, "que_jiaofeng_cards", 0)
        room:setPlayerMark(player, "@que_jiaofeng_cards", 0)
    end,
})


que_jiaofeng:addEffect(fk.TurnEnd, {
    anim_type = "support",
    can_trigger = function(self, event, target, player, data)
        return target ~= player and player:hasSkill(que_jiaofeng.name) and player:getMark("@que_jiaofeng-turn") > 0 and
            target:getMark("@@que_jiaofeng-turn") > 0
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local cards = {}
        local use_cards = {}
        room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
                if move.toArea == Card.Processing then
                    if move.from == target and (move.moveReason == fk.ReasonUse or move.moveReason == fk.ReasonResponse) then
                        for _, info in ipairs(move.moveInfo) do
                            if info.fromArea == Card.PlayerHand then
                                table.insertIfNeed(use_cards, info.cardId)
                            end
                        end
                    end
                elseif move.toArea == Card.DiscardPile then
                    if move.from == target then
                        if move.moveReason == fk.ReasonDiscard then
                            for _, info in ipairs(move.moveInfo) do
                                if info.fromArea == Card.PlayerHand then
                                    table.insertIfNeed(cards, info.cardId)
                                end
                            end
                        end
                    elseif #use_cards > 0 and (move.moveReason == fk.ReasonUse or move.moveReason == fk.ReasonResponse) then
                        for _, info in ipairs(move.moveInfo) do
                            if info.fromArea == Card.Processing and table.contains(use_cards, info.cardId) then
                                table.insertIfNeed(cards, info.cardId)
                            end
                        end
                    end
                end
            end
            return false
        end, Player.HistoryTurn)
        if #cards > 0 then
            local n = 0
            for _, id in ipairs(cards) do
                local num = Fk:getCardById(id).number
                n = n + num
            end
            room:setPlayerMark(target, "@que_jiaofeng-turn", n)
            local markp = player:getMark("@que_jiaofeng-turn")
            local markt = target:getMark("@que_jiaofeng-turn")
            local max
            local min
            if markp == markt then return end
            if markp > markt then
                max = player
                min = target
            else
                max = target
                min = player
                room:addPlayerMark(player, "que_jiaofeng_cards")
                room:setPlayerMark(player, "@que_jiaofeng_cards", player.hp + player:getMark("que_jiaofeng_cards"))
            end
            local use = room:askToUseRealCard(max, {
                pattern = cards,
                skill_name = que_jiaofeng.name,
                prompt = "交锋：是否选择使用其中一张？",
                extra_data = {
                    bypass_times = true,
                    extraUse = true,
                    expand_pile = cards,
                }
            })
            if use then
                table.removeOne(cards, use.card.id)
            end
            if #cards > 0 then
                if room:askToSkillInvoke(player, { skill_name = que_jiaofeng.name, prompt = "交锋：是否令" .. min.id .. "获得剩余牌中的一张" }) then
                    local card = room:askToChooseCard(min, {
                        target = min,
                        flag = { card_data = { { "toObtain", cards } } },
                        skill_name = que_jiaofeng.name,
                        prompt = "交锋：请选择获得其中一张",
                    })
                    room:moveCardTo(card, Card.PlayerHand, min, fk.ReasonJustMove, que_jiaofeng.name, nil, true, min)
                end
            end
        end
    end,
})

return que_jiaofeng
